var sharedData=require("dataMgr");
var helper=require("Helpers");
//处理弹出框脚本，升级角色属性
cc.Class({
    extends: cc.Component,

    properties: {
        allScoreLabel:{
            default:null,
            type:cc.Label
        },
        addScoreLabel:{
            default:null,
            type:cc.Label
        },
        speedLevelLabel:{
            default:null,
            type:cc.Label
        },
        powerLevelLabel:{
            default:null,
            type:cc.Label
        },
        speedScoreLabel:{
            default:null,
            type:cc.Label
        },
        powerScoreLabel:{
            default:null,
            type:cc.Label
        },
        upSpeedBnt:{
            default:null,
            type:cc.Node
        },
        upPowerBnt:{
            default:null,
            type:cc.Node
        },
        restartBnt:{
            default:null,
            type:cc.Node
        }
    },

    startShow:function(isFinish,score){
        //开始显示弹框
        this._isFinish=isFinish;
        this.node.active=true;
        this.node.scale=0;
        this.node.runAction(cc.scaleTo(0.2,1,1));
        //给各个文字赋值
        this.addScoreLabel.node.active=isFinish;
        this.restartBnt.active=isFinish;
        if(score>0){
            this.addScoreLabel.string="+"+helper.getKNum(score)+"pts";
        }
        this.updateLevel();
    },

    restartGame:function(){
        this.dismissDialog();
        this._game.restartGame(true);
    },

    updateLevel:function(){
        this.allScoreLabel.string=helper.getKNum(sharedData.roleGold);
        this.speedLevelLabel.string="level "+sharedData.curRoleSpeedInfo.level.toString();
        this.powerLevelLabel.string="level "+sharedData.curRolePowerInfo.level.toString();
        this.speedScoreLabel.string=helper.getKNum(sharedData.curRoleSpeedInfo.gold);
        this.powerScoreLabel.string=helper.getKNum(sharedData.curRolePowerInfo.gold);
    },

    //弹出升级框
    createUpdateDialog:function(game){
        this._game=game;
        this.startShow(false,0);
     },
 
     //弹出结束框
     createFinishDialog:function(score,game){
         this._game=game;
        sharedData.addRoleGold(score);
        this.startShow(true,score);
     },

     dismissDialog:function(){
       this.node.runAction(cc.sequence(cc.scaleTo(0.2,0,0),cc.callFunc(function(){
            this.node.active=false;
       },this)));
     },

    // LIFE-CYCLE CALLBACKS:
    onLoad:function() {
       //这里设置按钮的一些回调
       this.upSpeedBnt.getComponent("button")._callback=function(){
        if(sharedData.upSpeedLevel()){
            //如果升级成功的话,更新对话框ui
            this.updateLevel();
            //更新主页的显示
            this._game.resetRoleInfo();
        }
    }.bind(this);
    this.upPowerBnt.getComponent("button")._callback=function(){
        if(sharedData.upPowerLevel()){
            //如果升级成功的话
            this.updateLevel();
            this._game.resetRoleInfo();
        }
    }.bind(this);
    this.restartBnt.getComponent("button")._callback=function(){
        //点击重新开始游戏的话
        this.restartGame();
    }.bind(this);

    this.node.on(cc.Node.EventType.TOUCH_START, function (e) {
        //直接在这里dismiss掉
        if(!this._isFinish){
            this.dismissDialog();
        } 
       e.stopPropagation();
    }.bind(this), this);

    this.node.on(cc.Node.EventType.TOUCH_MOVE, function (e) {
        e.stopPropagation();
    }.bind(this), this);

    this.node.on(cc.Node.EventType.TOUCH_END, function (e) {
        e.stopPropagation();
    }.bind(this), this);

    this.node.on(cc.Node.EventType.TOUCH_CANCEL, function (e) {
        e.stopPropagation();
    }.bind(this), this);
    },
});
